<template>
  <div class="login-container">
    <van-nav-bar title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <van-form ref="loginFrom" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="moblie"
        placeholder="请输入手机号"
        :rules="userFromRules.mobile"
        type="number"
        maxlength="11"
        autofocus
      >
        <!-- type="number" 必须数字类型 ；maxlength 最大长度  -->
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请填写验证码"
        :rules="userFromRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="1000 * 60"
            format=" ss s 后重试"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <van-button
            class="send-sms-btn"
            size="small"
            round
            type="default"
            native-type="button"
            @click="onSendSms"
            v-else
            >获取验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
import { debounce } from 'lodash'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: '19951503960',
        code: '246810'
      },
      userFromRules: {
        // 手机号码和验证码校验规则
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      isCountDownShow: false,
      time: ''
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit() {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 为 0 时 持续展示 ，成功后停止
      })
      try {
        const { data } = await login(this.user)
        this.$toast.success('登录成功')
        this.$store.commit('setUser', data.data)
        this.$router.back()
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast.fail('手机号或验证码不正确')
        } else {
          this.$toast.fail('登录失败，稍后重试')
        }
      }
    },
    // debounce防抖
    onSendSms: debounce(async function() {
      try {
        await this.$refs.loginFrom.validate('moblie')
        console.log('验证通过')
      } catch (error) {
        return console.log('验证失败', error)
      }
      //   倒计时
      this.isCountDownShow = true
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (error) {
        this.isCountDownShow = false
        if (error.response.status === 429) {
          this.$toast('发送验证码过于频繁,请稍后再试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    }, 200)
    // async onSendSms() {
    //   try {
    //     await this.$refs.loginFrom.validate('moblie')
    //     console.log('验证通过')
    //   } catch (error) {
    //     return console.log('验证失败', error)
    //   }
    //   //   倒计时
    //   this.isCountDownShow = true
    //   try {
    //     await sendSms(this.user.mobile)
    //     this.$toast('发送成功')
    //   } catch (error) {
    //     this.isCountDownShow = false
    //     if (error.response.status === 429) {
    //       this.$toast('发送验证码过于频繁,请稍后再试')
    //     } else {
    //       this.$toast('发送失败，请稍后重试')
    //     }
    //   }
    // }
  }
}
</script>

<style scoped lang="less">
.send-sms-btn {
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
}
.iconfont {
  font-size: 37px;
}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
